package mobi.greenplate.theweb.user;

import org.deltaset.faces.FacesUtils;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import javax.inject.Named;
import mobi.greenplate.theweb.domain.User;
import mobi.greenplate.theweb.repository.UserRepository;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.WebApplicationContext;

/**
 *
 * @author Dimitar Makariev
 */
@ManagedBean("loginBean")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class LoginBean {

    private final AuthenticationManager am;
    private final UserRepository userRepository;

    @Inject
    public LoginBean(@Named("authenticationManager") AuthenticationManager am,@Named("userRepository") UserRepository userRepository) {
        this.am = am;
        this.userRepository=userRepository;
    }

    public String login() {
        try {
            Authentication request = new UsernamePasswordAuthenticationToken(this.getUserName(), getPassword());
            Authentication result = am.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);

        } catch (AuthenticationException e) {
            String loginFailedMessage = FacesUtils.getBundleKey("msg", "login.failed");
            FacesUtils.addErrorMessage(loginFailedMessage);
            return null;
        }        
        //TODO: get redirect from spring security configuration
        return "/welcome?faces-redirect=true";
    }
    private String userName;
    private String password;
    private String repassword;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

     public String getRepassword() {
        return repassword;
    }

    public void setRepassword(String repassword) {
        this.repassword = repassword;
    }

    public String register() {
        if (!this.getPassword().equals(this.getRepassword())) {
            FacesUtils.addErrorMessage("passowrd doesn't match");
            return null;
        }
        if(this.getPassword().length()<5){
            FacesUtils.addErrorMessage("passowrd too short");
            return null;
        }
        if (userRepository.isUserRegistered(this.getUserName())) {
            FacesUtils.addErrorMessage("change the user name ");
            return null;
        }
       
        userRepository.save(new User(userName,password));

        return this.login();
    }


    public Long getCurrentUserId(){
        return UserUtils.getUserId();
    }

     public String getCurrentUserName(){
        return UserUtils.getUserName();
    }

      public String getCurrentUserPassword(){
        return UserUtils.getPassword();
    }
}
